package your_car_status

import org.springframework.dao.DataIntegrityViolationException

class MechanicalTechnicianController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }
    def newVehicle(){  
        def cust = MechanicalTechnician.get(session.data.id).repairShop.customer
        [vehicleInstance: new Vehicle(params), customers:cust]
    }
    def saveVehicle(){
        def vehicleInstance = new Vehicle(params)
        def cust = MechanicalTechnician.get(session.data.id).repairShop.customer
        if (!vehicleInstance.save(flush: true)) {
            render(view: "newVehicle", model: [vehicleInstance: vehicleInstance, customers:cust])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), vehicleInstance.id])
        redirect(action: "newVehicle", id: vehicleInstance.id)
    }
    def listVehicle(){
        def cust = MechanicalTechnician.get(session.data.id).repairShop.customer
        def veh=[]
        for(c in cust){
            veh.addAll(c.vehicle)
        }
        [vehicleInstanceList:veh]
    }
    
    def searchVehicle (String licensePlate){
        def vehicleInstance
        licensePlate = licensePlate.toUpperCase()
        def cust = MechanicalTechnician.get(session.data.id).repairShop.customer
        def veh=[]
        for(c in cust){
            veh.addAll(c.vehicle)
        }
        for (Vehicle vehicle: veh){
            if(vehicle.getLicensePlate().equals(licensePlate)){
            vehicleInstance = vehicle
            }
        }   
        [vehicleInstanceList: vehicleInstance]
    }
    
    def editVehicle(Long id){
        def vehicleInstance = Vehicle.get(id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "listVehicle")
            return
        }

        [vehicleInstance: vehicleInstance]
    }
    
    def showVehicle(Long id) {
        def vehicleInstance = Vehicle.get(id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "list")
            return
        }

        [vehicleInstance: vehicleInstance]
    }
    
    def updateVehicle(Long id, Long version) {
        def vehicleInstance = Vehicle.get(id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (vehicleInstance.version > version) {
                vehicleInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'vehicle.label', default: 'Vehicle')] as Object[],
                          "Another user has updated this Vehicle while you were editing")
                render(view: "editVehicle", model: [vehicleInstance: vehicleInstance])
                return
            }
        }

        vehicleInstance.properties = params

        if (!vehicleInstance.save(flush: true)) {
            render(view: "editVehicle", model: [vehicleInstance: vehicleInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), vehicleInstance.id])
        redirect(action: "editVehicle", id: vehicleInstance.id)
    }
     
    def deleteVehicle(Long id) {
        def vehicleInstance = Vehicle.get(id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "listVehicle")
            return
        }

        try {
            vehicleInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "listVehicle")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def newCustomer(){
        [customerInstance: new Customer()]
    }
    
    def listCustomer(){
        [customerInstanceList: Customer.list()]
    }
    
    def saveCustomer(){
        def customerInstance = new Customer(params)
        if (!customerInstance.save(flush: true)) {
            render(view: "newCustomer", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "newCustomer", id: customerInstance.id)
    }
    
    def editCustomer(Long id){
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "listCustomer")
            return
        }

        [customerInstance: customerInstance]
    }
    
    def updateCustomer(Long id, Long version) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (customerInstance.version > version) {
                customerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'customer.label', default: 'Customer')] as Object[],
                          "Another user has updated this Customer while you were editing")
                render(view: "editCustomer", model: [customerInstance: customerInstance])
                return
            }
        }

        customerInstance.properties = params

        if (!customerInstance.save(flush: true)) {
            render(view: "editCustomer", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "editCustomer", id: customerInstance.id)
    }
    
    def showCustomer(Long id) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        [customerInstance: customerInstance]
    }
    
    
    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [mechanicalTechnicianInstanceList: MechanicalTechnician.list(params), mechanicalTechnicianInstanceTotal: MechanicalTechnician.count()]
    }

    def create() {
        [mechanicalTechnicianInstance: new MechanicalTechnician(params)]
    }

    def save() {
        def mechanicalTechnicianInstance = new MechanicalTechnician(params)
        if (!mechanicalTechnicianInstance.save(flush: true)) {
            render(view: "create", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), mechanicalTechnicianInstance.id])
        redirect(action: "show", id: mechanicalTechnicianInstance.id)
    }

    def show(Long id) {
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(action: "list")
            return
        }

        [mechanicalTechnicianInstance: mechanicalTechnicianInstance]
    }

    def edit(Long id) {
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(controller:'LogIn',action: "logout")
            return
        }
        [mechanicalTechnicianInstance: mechanicalTechnicianInstance]
    }

     def editMechanicalTechnician(Long id) {
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(controller:'LogIn',action: "logout")
            return
        }
        [mechanicalTechnicianInstance: mechanicalTechnicianInstance]
    }
    
    def update(Long id, Long version) {
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(controller:'LogIn',action: "logout")
            return
        }

        if (version != null) {
            if (mechanicalTechnicianInstance.version > version) {
                mechanicalTechnicianInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician')] as Object[],
                          "Another user has updated this MechanicalTechnician while you were editing")
                render(view: "editMechanicalTechnician", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
                return
            }
        }

        mechanicalTechnicianInstance.properties = params

        if (!mechanicalTechnicianInstance.save(flush: true)) {
            render(view: "edit", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), mechanicalTechnicianInstance.id])
        redirect(action: "edit", id: mechanicalTechnicianInstance.id)
    }

    def delete(Long id) {
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(action: "listVehicle")
            return
        }

        try {
            mechanicalTechnicianInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(action: "listVehicle")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(action: "show", id: id)
        }
    }
    def listService(Long id) {
        def veh=Vehicle.get(id).services;
        [servicesInstanceList: veh, servicesInstanceTotal: Services.count(), carId:id]
    }

    def newService(Long id) {
        def servicios=MechanicalTechnician.get(session.data.id).repairShop.servicePackages;
        [servicesInstance: new Services(params),mechId:session.data.id,carId:id,packages:servicios]
    }

    def saveService() {
        def servicesInstance = new Services(params)
        if (!servicesInstance.save(flush: true)) {
            render(view: "newService", model: [servicesInstance: servicesInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'services.label', default: 'Services'), servicesInstance.id])
        redirect(action: "newService", id: params.vehicle.id)
    }
    
    def editService(Long id) {
        def servicesInstance = Services.get(id)
        if (!servicesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "editService")
            return
        }

        [servicesInstance: servicesInstance, mechId:servicesInstance.mechanicalTechnician.id,carId: servicesInstance.vehicle.id]
    }

    def updateService(Long id, Long version) {
        def servicesInstance = Services.get(id)
        if (!servicesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "editService")
            return
        }

        if (version != null) {
            if (servicesInstance.version > version) {
                servicesInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'services.label', default: 'Services')] as Object[],
                          "Another user has updated this Services while you were editing")
                render(view: "editService", model: [servicesInstance: servicesInstance])
                return
            }
        }

        servicesInstance.properties = params

        if (!servicesInstance.save(flush: true)) {
            render(view: "editService", model: [servicesInstance: servicesInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'services.label', default: 'Services'), servicesInstance.id])
        redirect(action: "editService", id: servicesInstance.id)
    }

    def deleteService(Long id) {
        def servicesInstance = Services.get(id)
        if (!servicesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "listService")
            return
        }

        try {
            servicesInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "listService")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "listService", id: id)
        }
    }
    def listDiagnostic(Long id) {
        def veh=Vehicle.get(id).diagnostic;
        [diagnosticInstanceList: veh, diagnosticInstanceTotal: Diagnostic.count(),carId:id]
    }

    def newDiagnostic(Long id) {
        [diagnosticInstance: new Diagnostic(params),carId:id]
    }

    def saveDiagnostic() {
        params.date=new Date()
        def diagnosticInstance = new Diagnostic(params)
        if (!diagnosticInstance.save(flush: true)) {
            render(view: "newDiagnostic", model: [diagnosticInstance: diagnosticInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), diagnosticInstance.id])
        redirect(action: "newDiagnostic", id: params.vehicle.id)
    }
    
    def editDiagnostic(Long id) {
        def diagnosticInstance = Diagnostic.get(id)
        if (!diagnosticInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "editDiagnostic")
            return
        }

        [diagnosticInstance: diagnosticInstance,carId:diagnosticInstance.vehicle.id]
    }

    def updateDiagnostic(Long id, Long version) {
        def diagnosticInstance = Diagnostic.get(id)
        if (!diagnosticInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "editDiagnostic")
            return
        }

        if (version != null) {
            if (diagnosticInstance.version > version) {
                diagnosticInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'diagnostic.label', default: 'Diagnostic')] as Object[],
                          "Another user has updated this Diagnostic while you were editing")
                render(view: "editDiagnostic", model: [diagnosticInstance: diagnosticInstance])
                return
            }
        }

        diagnosticInstance.properties = params

        if (!diagnosticInstance.save(flush: true)) {
            render(view: "editDiagnostic", model: [diagnosticInstance: diagnosticInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), diagnosticInstance.id])
        redirect(action: "editDiagnostic", id: diagnosticInstance.id)
    }

    def deleteDiagnostic(Long id) {
        def diagnosticInstance = Diagnostic.get(id)
        if (!diagnosticInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "listDiagnostic")
            return
        }

        try {
            diagnosticInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "listDiagnostic")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "listDiagnostic", id: id)
        }
    }
    
    def listInventory(Long id) {
        def veh=Vehicle.get(id).inventory;
        [inventoryInstanceList: veh, inventoryInstanceTotal: Inventory.count(),carId:id]
    }

    def newInventory(Long id) {
        [inventoryInstance: new Inventory(params),mechID:session.data.id,carId:id]
    }

    def saveInventory() {
        def inventoryInstance = new Inventory(params)
        if (!inventoryInstance.save(flush: true)) {
            render(view: "newInventory", model: [inventoryInstance: inventoryInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'inventory.label', default: 'Inventory'), inventoryInstance.id])
        redirect(action: "newInventory", id: params.vehicle.id)
    }
    
    def editInventory(Long id) {
        def inventoryInstance = Inventory.get(id)
        if (!inventoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "editInventory")
            return
        }

        [inventoryInstance: inventoryInstance,mechID:inventoryInstance.mechanicalTechnician.id,carId:inventoryInstance.vehicle.id]
    }

    def updateInventory(Long id, Long version) {
        def inventoryInstance = Inventory.get(id)
        if (!inventoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "editInventory")
            return
        }

        if (version != null) {
            if (inventoryInstance.version > version) {
                inventoryInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'inventory.label', default: 'Inventory')] as Object[],
                          "Another user has updated this Inventory while you were editing")
                render(view: "editInventory", model: [inventoryInstance: inventoryInstance])
                return
            }
        }

        inventoryInstance.properties = params

        if (!inventoryInstance.save(flush: true)) {
            render(view: "editInventory", model: [inventoryInstance: inventoryInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'inventory.label', default: 'Inventory'), inventoryInstance.id])
        redirect(action: "editInventory", id: inventoryInstance.id)
    }

    def deleteInventory(Long id) {
        def inventoryInstance = Inventory.get(id)
        if (!inventoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "listInventory")
            return
        }

        try {
            inventoryInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "listInventory")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "listInventory", id: id)
        }
    }
    
    
}
